package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.WuLiuBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WuLiuListActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private String budget_id = "";
    private TextView btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wu_liu_list);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("安装进度");
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("发布");
        btn = (TextView) findViewById(R.id.btn);
        if ("0".equals(getIntent().getStringExtra("type"))) {
            submit.setVisibility(View.VISIBLE);
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(this);
//            btn.setVisibility(View.VISIBLE);
//            btn.setOnClickListener(this);
        }
//        if (1 == getIntent().getIntExtra("bao", 0)) {
//
//        }
        submit.setOnClickListener(this);
        budget_id = getIntent().getStringExtra("budget_id");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    }

    @Override
    protected void onResume() {
        upLoad();
        super.onResume();
    }

    private List<WuLiuBean.InfoEntity> info1 = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", budget_id);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_LOOKPROGRESS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        WuLiuBean bean = gson.fromJson(result, WuLiuBean.class);
                        info1 = bean.getInfo();
                        initData();
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });
    }

    private void initData() {
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_wuliu;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                RecyclerView recyclerView = holder.getView(R.id.recycler);
                recyclerView.setLayoutManager(new LinearLayoutManager(WuLiuListActivity.this, LinearLayoutManager.HORIZONTAL, false));
                holder.setTextView(R.id.time, info1.get(position).getAdd_time());
                holder.setTextView(R.id.content, info1.get(position).getContent());


                recyclerView.setAdapter(new BaseRVAdapter(WuLiuListActivity.this, info1.get(position).getImg()) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_wuliu_img;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position2) {
                        x.image().bind(holder.getImageView(R.id.img), info1.get(position).getImg().get(position2));
                    }

                });


            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                startActivity(new Intent(this, FaBuActivity.class).putExtra("budget_id", budget_id));
                break;
            case R.id.btn:
                junGong();
                break;
        }
    }

    private void junGong() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", budget_id);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_ENDDECORATE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
                        MyToast(info);
//                        MyApplication.getInstance().destoryActivity("inssss");
                        EventBus.getDefault().post(new Finish1());
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });

    }

}
